package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.bean.Food;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

// 定义一个添加食物信息记录的类
public class InputFoodActivity extends AppCompatActivity {

    private EditText editTextName, editTextExpiryDate, editTextCount;
    private Button buttonSave;

    // 定义onCreate()方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_food);

        // 对象赋值（findViewById()）
        editTextName = findViewById(R.id.editTextName);
        editTextExpiryDate = findViewById(R.id.editTextExpiryDate);
        editTextCount = findViewById(R.id.editTextCount);
        buttonSave = findViewById(R.id.buttonSave);

        // 设置监听器
        buttonSave.setOnClickListener(new View.OnClickListener() {
            // 重写点击事件
            @Override
            public void onClick(View v) {
                // 成员变量赋值（.getText().toString()）
                String name = editTextName.getText().toString();
                String expiryDate = editTextExpiryDate.getText().toString();
                String countStr = editTextCount.getText().toString();

                // 判断输入是否有空，有空提示补充输入
                if (name.isEmpty() || expiryDate.isEmpty() || countStr.isEmpty()) {
                    Toast.makeText(InputFoodActivity.this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                    return;
                }

                int count;

                try { // 数字格式异常捕获和抛出
                    // 字符串转化为整型
                    count = Integer.parseInt(countStr);
                } catch (NumberFormatException e) {
                    // 抛出格式不正确的提示
                    Toast.makeText(InputFoodActivity.this, "个数格式不正确", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 自动获取当前日期作为存储日期
                String storeDate = getCurrentDate();

                // 创建食物对象并插入数据库
                Food food = new Food();
                food.setName(name);
                food.setStoreDate(storeDate);
                food.setExpiryDate(expiryDate);
                food.setCount(count); // 设置个数

                MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(InputFoodActivity.this);
                long result = dbHelper.insertFood(food);

                if (result != -1) {
                    Toast.makeText(InputFoodActivity.this, "食物添加成功", Toast.LENGTH_SHORT).show();
                    finish(); // 关闭当前页面
                } else {
                    Toast.makeText(InputFoodActivity.this, "食物添加失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private String getCurrentDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return dateFormat.format(new Date());
    }
}
